package com.toe.recommend.webapi.util;

import java.util.*;

public class HashUtil<T> {

    /**
     * 根据最大value寻找key
     * @param map
     * @param value
     * @param <T>
     * @return
     */
    public static <T> List<T> getKey(Map<T, Double> map, List<Double> value){
        List<T> keys = new ArrayList<>();
        for (Map.Entry<T, Double> entry : map.entrySet()) {
            for (Double v : value) {
                if (v.equals(entry.getValue())) {
                    keys.add(entry.getKey());
                    break;
                }
            }
        }
        return keys;
    }



    /**
     * 遍历寻找最大value
     * @param map
     * @param <T>
     * @return
     */
    public static <T> List<Double> getMaxValue(Map<T, Double> map, int count) {
        if (map.size() == 0)
            return null;
        Collection<Double> c = map.values();
        Object[] obj = c.toArray();
        Double[] ds = new Double[obj.length];

        for (int i = 0; i < obj.length; i++) {
            ds[i] = (Double) obj[i];
//            System.out.println("ds: "+ds[i]);
        }

        Arrays.sort(ds);
        List<Double> list = new ArrayList<>();

        int num = Math.min(count, ds.length);
        System.out.println("==============hashUtil=============");
        System.out.println("count: "+count+"    ds.length: "+ds.length+"     num: "+num);
        for (int i = 1; i <= num; i++) {
            list.add(ds[ds.length - i]);
        }
//        list.forEach(System.out::println);
        return list;
    }
    
}
